package com.ouken.tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;



public class ConnectServer {
	public static synchronized String connect(String httpUrl, List params) {
		String result = new String();
		HttpPost httpRequest = new HttpPost(httpUrl);
		
		
		try {
			HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
			httpRequest.setEntity(entity);
			HttpClient client = new DefaultHttpClient();
			client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
			HttpResponse response = client.execute(httpRequest);
			
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				InputStream in = response.getEntity().getContent();
				Reader  inputStreamReader = new InputStreamReader(in); 
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader,8192);  
				
				String s;    
				  while((s = bufferedReader.readLine())!=null){    
					  result += s;    
				  } 
			}
			//textView.setText("123");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		System.out.println(httpUrl.substring(15)+":"+result.toString());
		
		result =result.replaceAll("(\\()", "");
		result =result.replaceAll("(\\))", "");
		result =result.replaceAll("(\\')", "");
		
		return result;
		
	}
	public static synchronized Bitmap getBitmap(String httpUrl){
		URL myFileUrl = null;   
		Bitmap bitmap = null;   
		try {   
		myFileUrl = new URL("http://"+getServerNoport()+"/img/"+httpUrl);   
		} catch (MalformedURLException e) {   
		e.printStackTrace();   
		}   
		try {   
		HttpURLConnection conn = (HttpURLConnection) 
		myFileUrl.openConnection();   
		conn.setDoInput(true);   
		conn.connect();   
		InputStream is = conn.getInputStream();   
		bitmap = BitmapFactory.decodeStream(is);   
		is.close();   
		} catch (IOException e) {   
		e.printStackTrace();   
		}   
		return bitmap;   
	}
	public static synchronized String getServerAdd(){
		return "192.168.11.189:8090";
		//return "36.55.231.19:8090";
		
	}
	public static synchronized String getServerNoport(){
		return "192.168.11.189";
		//return "36.55.231.19";
	}

}
